package com.yxjx.duoxue;

import java.util.ArrayList;

import com.thirdparty.neutra.autoscrollloopviewpager.AutoScrollLoopViewPager;
import com.thirdparty.trinea.autoscrollviewpager.AutoScrollViewPager;
import com.yxjx.duoxue.mta.stat.StatHelper;
import com.yxjx.duoxue.push.PushHelper;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class GuidingActivity extends Activity {
	protected float lastX;
	protected float lastY;
	protected float nowX;
	protected float nowY;
	private GuidingPageAdapter adpater;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.segment_guiding_pages);
		mContext = this;

		StatHelper.addKVEvent(mContext, "event_app_loading");
		initAdIndicator((LinearLayout) findViewById(R.id.ad_indicator), 3);
		adpater = new GuidingPageAdapter();
		final AutoScrollViewPager gudingPagers = (AutoScrollViewPager) findViewById(R.id.guid_pager);
		gudingPagers.setAdapter(adpater);
		gudingPagers.setInterval(3000);
		gudingPagers.startAutoScroll(3000);
		
		gudingPagers.setOnPageChangeListener(new OnPageChangeListener() {
			
			private int previousPosition = -1;
			
			@Override
			public void onPageSelected(int arg0) {
			}
			
			@Override
			public void onPageScrolled(int position, float arg1, int arg2) {
				if (arg1 == 0.0) {
					setAdIndicator((LinearLayout) findViewById(R.id.ad_indicator), position);
				}
				if (arg1 == 0.0 && position == adpater.getCount() - 1) {
					gudingPagers.stopAutoScroll();
				}
				if (position != previousPosition) {
					StatHelper.addKVEvent(mContext, "event_guiding_" + (position + 1));
					previousPosition = position;
				}
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			
		});
		
	}
	
	private void initAdIndicator(LinearLayout indicator, int count) {
		for (int i = 0; i < count; i++) {
			ImageView child = new ImageView(this);
			child.setBackgroundResource(R.drawable.selector_guiding_indicator);
			indicator.addView(child);
		}
	}
	
	private void setAdIndicator(LinearLayout indicator, int index) {
		for (int i = 0; i < indicator.getChildCount(); i++) {
			if (i == index) {
				indicator.getChildAt(i).setSelected(true);
			} else {
				indicator.getChildAt(i).setSelected(false);
			}
		}
	}
	class GuidingPageAdapter extends PagerAdapter {
		
		public GuidingPageAdapter() {
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			
			View item = getViewItem(position);
			container.addView(item);
			return item;
		}

		private View getViewItem(int position) {
			View item = null;
			if (position == 0) {
				item = LayoutInflater.from(GuidingActivity.this).inflate(R.layout.guiding_page_1,
						null);
			} else if (position == 1) {
				item = LayoutInflater.from(GuidingActivity.this).inflate(R.layout.guiding_page_2,
						null);
			} else if (position == 2) {
				item = LayoutInflater.from(GuidingActivity.this).inflate(R.layout.guiding_page_3,
						null);
				item.setOnTouchListener(new View.OnTouchListener() {
					
					@Override
					public boolean onTouch(View view, MotionEvent event) {
						switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN:
							lastX = event.getX();
							lastY = event.getY();
							Utils.logd("move down lastX = " + lastX + ", lastY = " + lastY + ", nowX = " + nowX + ", nowY = " + nowY);
							
							break;
						case MotionEvent.ACTION_MOVE:
							Utils.logd("move start lastX = " + lastX + ", lastY = " + lastY + ", nowX = " + nowX + ", nowY = " + nowY);
							nowX = event.getX();
							nowY = event.getY();
							if ((lastX > (nowX + 50)) && ((lastX - nowX) > Math.abs(nowY - lastY))) {
								// 向左
								Utils.logd("start lastX = " + lastX + ", lastY = " + lastY + ", nowX = " + nowX + ", nowY = " + nowY);
								finishGuidingInternal(view);
							}
						}
						return false;
					}
				});
			}
			return item;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		// PUSH Activity
		try {
			Bundle bundle = getIntent().getExtras();
			String custom = bundle.getString(Yx.KEY_PUSH_EXTRA_CUSTOM);
			getIntent().putExtra(Yx.KEY_PUSH_EXTRA_CUSTOM, "");
			PushHelper.pushActivity(this, custom);
		} catch (Exception e) {
			// do nothing
		}
	}
	
	private void finishGuidingInternal(View view) {
		startActivity(new Intent(GuidingActivity.this, HomePageActivity.class));
		GuidingActivity.this.finish();
	}
	
	public void finishGuiding(View view) {
		StatHelper.addKVEvent(mContext, "event_guiding_start");
		finishGuidingInternal(view);
	}
}
